<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <script src="js/jQuery.js"></script>
    <link rel="stylesheet" href="admin/css/pearCommon.css" />
    <link rel="stylesheet" href="component/layui/css/layui.css" />
    <script src="component/layui/layui.js"></script>
    <style>
        .layui-form-label:after{
            content:"*";
            color:red;
            position: absolute;
            top:10px;
            left:-3px;
        }
    </style>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">角色名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="actorName" lay-verify="required" autocomplete="off"  class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色描述</label>
                    <div class="layui-input-block">
                        <input type="text"  name="actorDescription"  autocomplete="off"  class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">管理用户权限</label>
                    <div class="layui-input-block">
                        <input name="authority" id="authority"  type="checkbox" lay-skin="switch" lay-text="开|关">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">共享文件组权限</label>
                    <div class="layui-input-block">
                        <input name="fileGroup" id="fileGroup"  type="checkbox" lay-skin="switch" lay-text="开|关">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">使用状态</label>
                    <div class="layui-input-block">
                        <input name="state" id="state"  type="checkbox" lay-skin="switch" lay-text="使用|禁用">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="actor-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script>
    let actor={};
    $.ajax({
        url: sessionStorage.getItem("rootPath")+ "/actors/selectActorList",
        method: "post",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(actor),
        success: function (data) {
            $(data.data).each(function (i, n) {
                $("#role").append("<option value='" + n.id + "' style='font-weight: bold;'>" +  n.actorName + "</option>");
            });
            layui.use('form', function () {
                var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                form.render('select');
            })
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log("失败" + XMLHttpRequest.status + ":" + textStatus + ":" + errorThrown);
        }
    })
    layui.use(['pearHash','form','jquery'],function(){
        var form = layui.form,layer=layui.layer;
        var $ = layui.jquery;
        var pearHash = layui.pearHash;
        //自定义验证规则
        form.verify({
            required: function(value,elem){
                if(!value){
                    return '必填项不能为空！'
                }
            }
        });
        form.on('submit(actor-save)', function(data){
            if(data.field.authority == "on"){
                data.field.authority = 1
            }else {
                data.field.authority = 0
            }
            if(data.field.fileGroup == "on"){
                data.field.fileGroup = 1
            }else {
                data.field.fileGroup = 0
            }
            if(data.field.state == "on"){
                data.field.state = 1
            }else {
                data.field.state = 0
            }
            data.field.createPeople=sessionStorage.getItem("id")
            $.ajax({
                url: sessionStorage.getItem("rootPath") + "/actors/add",
                data:JSON.stringify(data.field),
                dataType:'json',
                contentType: 'application/json;charset=utf-8',
                type:'post',
                async: false,
                timeout : 50000,
                success:function(res){
                    if(res==1){
                        layer.msg("添加角色成功!",{icon:1,time:1000},function(){
                            parent.getActorList();
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        });
                    }else {
                        layer.msg("添加角色失败！", {icon: 0, time: 1000});
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log("失败" + XMLHttpRequest.status + ":" + textStatus + ":" + errorThrown);
                }
            })
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>